package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.domain.StrategyComment;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.query.QueryObject;
import cn.wolfcode.luowowo.repository.StrategyCommentRepository;
import cn.wolfcode.luowowo.service.IStrategyCommentService;
import cn.wolfcode.luowowo.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {
    @Autowired
    private StrategyCommentRepository strategyCommentRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Override
    public void insert(StrategyComment strategyComment, String token) {
        strategyComment.setId(null);
        UserInfo user = userInfoRedisService.getUserByToken(token);
        strategyComment.setUserId(user.getId());
        strategyComment.setNickname(user.getNickname());
        strategyComment.setCity(user.getCity());
        strategyComment.setLevel(user.getLevel());
        strategyComment.setHeadImgUrl(user.getHeadImgUrl());
        strategyComment.setCreateTime(new Date());
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public void update(StrategyComment strategyComment) {
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public void delete(String id) {
        strategyCommentRepository.deleteById(id);
    }

    @Override
    public StrategyComment select(String id) {
        return strategyCommentRepository.findById(id).get();
    }

    @Override
    public Page<StrategyComment> query(QueryObject qo) {
        Query query = new Query();
        if (StringUtils.hasLength(qo.getKeyword())) {
            Criteria criteria = new Criteria();
            criteria.orOperator(
                    Criteria.where("strategyTitle").regex(".*" + qo.getKeyword() + ".*"),
                    Criteria.where("nickname").regex(".*" + qo.getKeyword() + ".*")
            );
            query.addCriteria(criteria);
        }
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize()
                , Sort.Direction.ASC, "_id");

        return DBHelper.query(mongoTemplate, StrategyComment.class, query, pageable);
    }

    @Override
    public Page<StrategyComment> queryByStrategyId(int currentPage, String strategyId) {
        Query query = new Query();

        query.addCriteria(Criteria.where("strategyId").is(strategyId));

        PageRequest pageable = PageRequest.of(currentPage - 1, 5
                , Sort.Direction.DESC, "_id");

        return DBHelper.query(mongoTemplate, StrategyComment.class, query, pageable);
    }

    @Override
    public void commentThumb(String cid, String sid, String token) {
        UserInfo user = userInfoRedisService.getUserByToken(token);
        StrategyComment sc = this.select(cid);
        if (!(sc.getThumbuplist().contains(user.getId()))) {
            sc.getThumbuplist().add(user.getId());
            sc.setThumbupnum(sc.getThumbupnum() + 1);
        } else {
            sc.getThumbuplist().remove(user.getId());
            sc.setThumbupnum(sc.getThumbupnum() - 1);

        }
        strategyCommentRepository.save(sc);
    }
}
